package com.baidu.controller;

import com.baidu.constant.MessageConstant;
import com.baidu.pojo.CheckGroup;
import com.baidu.pojo.PageResult;
import com.baidu.pojo.QueryPageBean;
import com.baidu.pojo.Result;
import com.baidu.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    @RequestMapping("/findAllByGroup")
    public Result findAllByGroup(@RequestBody QueryPageBean queryPageBean){
        PageResult<CheckGroup> checkGroupPageResult = checkGroupService.findAllByGroup(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupPageResult);
    }

    @PostMapping("/add")
    public Result add(Integer[] checkitemIds, @RequestBody CheckGroup checkGroup){
        checkGroupService.addByCheckGroup(checkGroup, checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @GetMapping("/findAllByGroupId")
    public Result findAllByGroupId(int id){
        CheckGroup allByGroupId = checkGroupService.findAllByGroupId(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, allByGroupId);
    }

    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup){
        System.out.println(checkGroup);
        int row = checkGroupService.update(checkGroup);
        if (row > 0){
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }
        return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
    }

    @GetMapping("/delete")
    public Result delete(Integer id){
        System.out.println(id);
        boolean b = checkGroupService.delete(id);
        if (b) {
            return new Result(b, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }
        return new Result(b, MessageConstant.DELETE_CHECKGROUP_FAIL);
    }

    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroup = checkGroupService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }
}
